<template>
  <div class="personal-center">
    <el-form ref="formRef" :model="userForm" label-width="100px">
      <el-form-item label="用户名">
        <el-input v-model="userForm.username" />
      </el-form-item>

      <el-form-item
        label="手机号"
        prop="phone_number"
        :rules="[
          { required: false, message: '请输入手机号', trigger: 'blur' },
          { validator: validatePhoneNumber, trigger: 'blur' }
        ]"
      >
        <el-input
          v-model="userForm.phone_number"
          maxlength="11"
          placeholder="请输入手机号"
          @input="handlePhoneInput"
        />
      </el-form-item>

      <el-form-item label="角色">
        <el-select v-model="userForm.role" placeholder="请选择角色">
          <el-option label="管理员" value="admin" />
          <el-option label="普通用户" value="user" />
        </el-select>
      </el-form-item>

      <el-form-item label="头像">
        <el-upload
          action=""
          :auto-upload="false"
          :show-file-list="false"
          accept="image/*"
          @change="handleAvatarChange"
        >
          <el-button type="primary">上传头像</el-button>
        </el-upload>
        <div v-if="userForm.avatar" class="avatar-preview">
          <img :src="userForm.avatar" alt="头像" width="60" height="60" >
        </div>
      </el-form-item>

      <el-form-item label="性别">
        <el-radio-group v-model="userForm.gender">
          <el-radio :label="0">男</el-radio>
          <el-radio :label="1">女</el-radio>
        </el-radio-group>
      </el-form-item>

      <el-form-item label="地址">
        <el-input v-model="userForm.address" placeholder="请输入地址" />
      </el-form-item>

      <el-form-item label="国家">
        <el-input v-model="userForm.country" placeholder="请输入国家" />
      </el-form-item>

      <el-form-item label="语言">
        <el-select v-model="userForm.language" placeholder="请选择语言">
          <el-option label="中文" value="zh-CN" />
          <el-option label="英文" value="en-US" />
        </el-select>
      </el-form-item>

      <el-form-item label="偏好设置">
        <el-input v-model="userForm.preferences" placeholder="" type="textarea" :rows="3" />
      </el-form-item>

      <el-form-item label="社交信息">
        <el-input v-model="userForm.social" placeholder="" type="textarea" :rows="3" />
      </el-form-item>

      <el-form-item>
        <el-button type="primary" @click="saveUserInfo">保存</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script setup>
import { updateUser } from '@w/apis/user'
import { ElMessage } from 'element-plus'

import useUserStore from '~stores/module/user'

// 获取表单引用
const formRef = ref()
const { userInfo } = useUserStore()

const userForm = reactive({
  username: userInfo.username,
  phone_number: userInfo.phone_number,
  role: userInfo.role,
  avatar: userInfo.avatar,
  gender: userInfo.gender,
  address: userInfo.address,
  country: userInfo.country,
  language: userInfo.language,
  preferences: userInfo.preferences,
  social: userInfo.social
})

// 手机号输入处理
const handlePhoneInput = (value) => {
  // 只保留数字
  userForm.phone_number = value.replace(/[^0-9]/g, '')
}

// 手机号验证规则
const validatePhoneNumber = (rule, value, callback) => {
  const reg = /^1[3-9]\d{9}$/
  if (!reg.test(value)) {
    callback(new Error('请输入正确的手机号'))
  } else {
    callback()
  }
}

// 头像上传处理
const handleAvatarChange = (file) => {
  if (file.raw) {
    const reader = new FileReader()
    reader.onload = (e) => {
      userForm.avatar = e.target.result
    }
    reader.readAsDataURL(file.raw)
  }
}

// 保存用户信息
const saveUserInfo = async() => {
  // 进行表单验证
  try {
    await formRef.value.validate()

    const res = await updateUser(userInfo.id, userForm)
    useUserStore().setUserInfo(userForm)
    ElMessage.success(res.message)
  } catch (error) {
    console.error('表单验证失败:', error)
  }
}
</script>

<style lang="scss" scoped>
.personal-center {
  padding: 20px;
  margin: 0 auto;
  max-width: 800px;

  .avatar-preview {
    display: inline-block;
    margin-top: 10px;
  }

  .el-form-item {
    margin-bottom: 20px;
  }
}
</style>
